package com.example.Controller;


import com.example.baseObj.AjaxResult;
import com.example.entity.Role;
import com.example.entity.Permission;
import com.example.repository.RoleRepository;
import com.example.repository.PermissionRepository;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    private static Logger logger = LoggerFactory.getLogger(PermissionController.class);


    @Autowired
    private PermissionRepository permissionRepository;

    @Autowired
    private RoleRepository roleRepository;


    @RequestMapping("/index")
    public String index(ModelMap map,HttpServletRequest req) throws Exception{


        return "permission/index";
    }


    @RequestMapping("/list")
    @ResponseBody
    public AjaxResult list(ModelMap map, HttpServletRequest req) throws Exception{

        List<Permission> permissions = permissionRepository.findAll();



        return new AjaxResult(true,"1",permissions);
    }

    @RequestMapping(value="/{id}")
    public String show(ModelMap model,@PathVariable Long id) {
        Permission permission = permissionRepository.findOne(id);
        model.addAttribute("permission",permission);
        return "permission/show";
    }


    @RequestMapping("/add")
    public String create(ModelMap model,HttpServletRequest request){

        String pid = request.getParameter("pid");
        String name = request.getParameter("name");

        model.put("pid",pid);
        model.put("name",name);


        return "permission/add";
    }

    @RequestMapping(value="/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(Permission permission) throws Exception{



        permissionRepository.save(permission);
        logger.info("新增->ID="+permission.getId());
        return "1";
    }

    @RequestMapping(value="/edit/{id}")
    public String update(ModelMap model,@PathVariable Long id){

        Permission permission = permissionRepository.findOne(id);

        Permission par_permission = permissionRepository.findOne(permission.getPid());



        model.addAttribute("permission",permission);
        model.addAttribute("pid",par_permission.getId());
        model.addAttribute("pname",par_permission.getName());

        return "permission/edit";
    }

    @RequestMapping(method = RequestMethod.POST, value="/update")
    @ResponseBody
    public String update(Permission permission) throws Exception{
        permissionRepository.save(permission);
        logger.info("修改->ID="+permission.getId());
        return "1";
    }

    @RequestMapping(value="/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable Long id) throws Exception{
        permissionRepository.delete(id);
        logger.info("删除->ID="+id);
        return "1";
    }

}
